#!/bin/bash
now=$(date '+%Y-%m-%d %H:%M:%S.%s')
tail -n 1000 /docker/makeblog/watch_log.log > /docker/makeblog/watch_log.log;
echo "$now"
echo " " >> /docker/makeblog/watch_log.log;
echo "################################" >> /docker/makeblog/watch_log.log;
echo "cron time: $now" >> /docker/makeblog/watch_log.log;
# 发送curl请求并将响应结果保存到变量response中
http_status=$(curl -Is "https://gitee.com/uidoer/melog/events.json" | head -n 1 | grep '200' | grep 'OK')
echo "http_status:$http_status"
if [ ${#http_status} -gt 0  ] ; then
   echo "is 200"
   echo "is 200" >> /docker/makeblog/watch_log.log
else
   echo "not 200,now exit"
   echo "not 200,now exit" >> /docker/makeblog/watch_log.log
   exit 1
fi 

response=$(curl -s "https://gitee.com/uidoer/melog/events.json")
# echo "$response"
# 使用jq从响应结果中提取所需的字符串值，并将其赋给变量value
new_value=$(echo "$response" | jq -r '.data.events[0].created_at'  )
old_value=$(head -n 1 /docker/makeblog/oldvalue.txt) 
echo "old value is:$old_value"
# 打印变量value的值
echo "new value is: $new_value"


if [ "$new_value" = "$old_value" ]; then
    echo "值相等"
    echo "值相等" >> /docker/makeblog/watch_log.log
else
    echo "值不相等" 
    node /docker/makeblog/rebuild_blog.js;
    echo "Build and Run melog  successfully."
    echo "$new_value" > /docker/makeblog/oldvalue.txt;
    echo "Build and Run melog  successfully." >> /docker/makeblog/watch_log.log
fi
